package dapps.web.proj;

import java.util.List;

import javax.servlet.ServletContext;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.hibernate.HibernateWebUtils;
import org.springside.modules.web.struts2.CRUDActionSupport;
import org.springside.modules.web.struts2.Struts2Utils;

import dapps.entity.user.Role;
import dapps.entity.user.User;
import dapps.service.ServiceException;
import dapps.service.user.RoleManager;
import dapps.service.user.UserManager;

@SuppressWarnings("serial")
@Results( { @Result(name = "url", location = "${url}", type = "redirect") }
)
public class CompAction extends CRUDActionSupport<User> {

	@Autowired
	private UserManager userManager;
	@Autowired
	private RoleManager roleManager;

	// 基本属性
	private User entity;
	private Long id;
	private Page<User> page = new Page<User>(15);//每页5条记录
	private String url;  
	
	  
	
	// 角色相关属性
	private List<Role> allRoles; //全部可选角色列表
	private List<Long> checkedRoleIds; //页面中钩选的角色id列表

	// 基本属性访问函数 //

	public User getModel() {
		return entity;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			entity = userManager.get(id);
		} else {
			entity = new User();
		}
	}
	public String getUrl() {  
	     return url;  
	}  
	
	public void setUrl(String url) {
		this.url = url;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Page<User> getPage() {
		return page;
	}

	// CRUD Action 函数 //

	@Override
	public String list() throws Exception {
		page = userManager.getAll(page);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		allRoles = roleManager.getAll();
		checkedRoleIds = entity.getRoleIds();
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		//根据页面上的checkbox 整合User的Roles Set
		HibernateWebUtils.mergeByCheckedIds(entity.getRoles(), checkedRoleIds, Role.class);
		userManager.save(entity);		
		refreshapplication();
		addActionMessage("保存用户成功");
		if(url.indexOf("?")>=0) url = url+"&dutycompid="+entity.getId();
		else url = url+"?dutycompid="+entity.getId();
		return "url";
	}

	
	@Override
	public String delete() throws Exception {
		try {
			userManager.delete(id);			
			refreshapplication();
			addActionMessage("删除用户成功");
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			addActionMessage(e.getMessage());
		}
		return RELOAD;
	}

	// 其他属性访问函数与Action函数 //

	public List<Role> getAllRoles() {
		return allRoles;
	}

	public List<Long> getCheckedRoleIds() {
		return checkedRoleIds;
	}

	public void setCheckedRoleIds(List<Long> checkedRoleIds) {
		this.checkedRoleIds = checkedRoleIds;
	}

	
	public void refreshapplication() throws Exception {
		ServletContext application = Struts2Utils.getApplication();
		if(application.getAttribute("alldutycomp")!=null){			
			synchronized(application){
				application.removeAttribute("alldutycomp");
				application.setAttribute("alldutycomp", userManager.getUsersOrderByRole("duty"));
			}
		}
	}
}
